package mirrormonkey.core.tools.listeners;

import mirrormonkey.core.EntityLifecycleListener;
import mirrormonkey.core.SingleClientTestProto;
import mirrormonkey.framework.entity.StaticEntityData;
import mirrormonkey.framework.entity.SyncEntity;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;

public class MakeVisibleListener implements EntityLifecycleListener {

	/**
	 * 
	 */
	private final SingleClientTestProto clientInstantiationTest;

	/**
	 * @param singleClientTestProto
	 */
	public MakeVisibleListener(SingleClientTestProto singleClientTestProto) {
		this.clientInstantiationTest = singleClientTestProto;
	}

	@Override
	public void entityInitialized(SyncEntity entity) {
		if (Geometry.class.isInstance(entity)) {
			Geometry g = (Geometry) entity;
			if (g.getMaterial() == null) {
				Material m = new Material(
						this.clientInstantiationTest.clientApplication
								.getAssetManager(),
						"Common/MatDefs/Misc/Unshaded.j3md");
				m.setColor("Color", ColorRGBA.Blue);
				g.setMaterial(m);
			}
			this.clientInstantiationTest.clientApplication.getRootNode()
					.attachChild(g);
		}
	}

	@Override
	public void entityReplaced(SyncEntity replacedEntity,
			StaticEntityData replacedStaticData, SyncEntity replacingEntity) {
		entityRemoved(replacedEntity);
	}

	@Override
	public void entityReplacing(SyncEntity replacedEntity,
			StaticEntityData replacedStaticData, SyncEntity replacingEntity) {
		entityInitialized(replacingEntity);
	}

	@Override
	public void entityRemoved(SyncEntity entity) {
		if (Geometry.class.isInstance(entity)) {
			Geometry g = (Geometry) entity;
			this.clientInstantiationTest.clientApplication.getRootNode()
					.detachChild(g);
		}
	}

}